﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Reflection; 
using CoolCode.Services;
using CoolCode.Services.LinqToSql;
using CoolCode.Ioc;
using CoolCode.Web;
using CoolCode.Web.Security;

namespace Kangxin.Mvc
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

            routes.MapRoute(
                "Test",                                              // Route name
                "Test/{action}/{page}",                           // URL with parameters
                new { controller = "Test", action = "Index", page = "" }  // Parameter defaults
            );

            routes.MapRoute(
                "aspx",                                              // Route name
                "Views/{controller}/{action}.aspx",                           // URL with parameters
                new { controller = "Test", action = "PagerTest", page = "" }  // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            RegisterServices(InjectTable.Injector);
            // RegisterAllControllersIn(Assembly.GetExecutingAssembly());
        }

        private static void RegisterServices(Injector injector)
        {
            //Common
            injector.Link<ITrackService, TrackService>();
            injector.Link<IMembershipService, MembershipService>();
            injector.Link<IMenuService, MenuService>();
            injector.Link<IMembership, DefaultMembership>();
            injector.Link<IFormsAuthentication, DefaultFormsAuthentication>();
            //Customerize
            injector.Link<IInspectionService, InspectionService>();
            injector.Link<ITipsService, TipsService>();
            injector.Link<ICriterionService>( ()=> new CriterionService(Config.AppData , new WebCacheStrategy()));
        }
    }

    //public class MvcApplication : NinjectHttpApplication //System.Web.HttpApplication
    //{
    //    public static void RegisterRoutes(RouteCollection routes)
    //    {
    //        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //        routes.MapRoute(
    //            "Default",                                              // Route name
    //            "{controller}/{action}/{id}",                           // URL with parameters
    //            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    //        );
    //    }

    //    protected override void  OnApplicationStarted()
    //    {
    //        RegisterRoutes(RouteTable.Routes);
    //       // RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    //    }

    //    protected override IKernel CreateKernel()
    //    {
    //        return new StandardKernel(new ServiceModule());
    //    }
    //}

    //internal class ServiceModule : NinjectModule
    //{
    //    public override void Load()
    //    {
    //        //Sapmle: Bind<IFoo>().To<Foo>(); 
    //        //  Bind<IMenuService>().To<MenuService>();
    //    }
    //}
}